package ua.khuta.myskidka;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends FragmentActivity implements OnClickListener {
	Database db;
	String DEBUG_TAG = "myLogs";
	Button btnNews, btnNewsFilter, btnDiscountCatalog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initialization();
		db = Database.getInstance(this);
		Intent intent1 = getIntent();
		boolean key = intent1.getBooleanExtra("registration", false);
		Log.d("myDebug", "key= " + key);
		if (db.checkRegistration() == false && key == true) {
			Log.d("myDebug", "tyt");
			Bundle bundle = new Bundle();
			bundle.putBoolean("flag", true);
			bundle.putString("user_first_name",
					intent1.getStringExtra("user_first_name"));
			bundle.putString("user_last_name",
					intent1.getStringExtra("user_last_name"));
			bundle.putString("user_f_id", intent1.getStringExtra("user_f_id"));
			Fragment fragReg = new FragmentRegistration();
			fragReg.setArguments(bundle);
			FragmentTransaction fragTrans = getSupportFragmentManager()
					.beginTransaction().replace(R.id.content_frame, fragReg);
			fragTrans.commit();
		}
		if (db.checkRegistration() == false && key == false) {
			Intent intent = new Intent(MainActivity.this, FragmentLogin.class);
			MainActivity.this.startActivity(intent);
			MainActivity.this.finish();
			// Intent intent = new In
			// Fragment fragReg = new FragmentLogin();
			// FragmentTransaction fragTrans = getSupportFragmentManager()
			// .beginTransaction().replace(R.id.content_frame, fragReg);
			// fragTrans.commit();
		}
		if (db.checkRegistration() == true) {

			Fragment fragNews = new FragmentNews();
			// Fragment fragNews= new FragmentFilterNews();
			// Fragment fragNews=new FragmentRegistration();
			FragmentTransaction fragTrans = getSupportFragmentManager()
					.beginTransaction().replace(R.id.content_frame, fragNews);
			fragTrans.commit();
		}
	}

	private void initialization() {
		btnNews = (Button) findViewById(R.id.btn_news);
		btnNews.setOnClickListener(this);
		btnNewsFilter = (Button) findViewById(R.id.btn_news_filter);
		btnNewsFilter.setOnClickListener(this);
		btnDiscountCatalog = (Button) findViewById(R.id.btn_discount_list);
		btnDiscountCatalog.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		Fragment newFragment;
		FragmentTransaction fragTrans;
		switch (v.getId()) {
		case R.id.btn_news:
			newFragment = new FragmentNews();
			fragTrans = getSupportFragmentManager().beginTransaction().replace(
					R.id.content_frame, newFragment);
			fragTrans.commit();
			break;
		case R.id.btn_news_filter:
			newFragment = new FragmentFilterNews();
			fragTrans = getSupportFragmentManager().beginTransaction().replace(
					R.id.content_frame, newFragment);
			fragTrans.commit();
			break;
		case R.id.btn_discount_list:
			newFragment = new FragmentCategoriesList();
			fragTrans = getSupportFragmentManager().beginTransaction().replace(
					R.id.content_frame, newFragment);
			fragTrans.commit();
			break;
		default:
			break;
		}

	}

}
